package net.csdn.business.auth.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.SneakyThrows;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.auth.exception.CloudAuth2Exception;


/**
 * @Desc: 针对OAuth2的异常进行统一返回
 * @ClassName: CloudAuth2ExceptionSerializer
 * @Author: yourleige@163.com
 * @Date: 2022/9/26 14:21
 */
public class CloudAuth2ExceptionSerializer extends StdSerializer<CloudAuth2Exception> {

	public CloudAuth2ExceptionSerializer() {
		super(CloudAuth2Exception.class);
	}

	@Override
	@SneakyThrows
	public void serialize(CloudAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {
		gen.writeStartObject();
		gen.writeObjectField("code", ResultCodeEnum.FAIL.getCode());
		gen.writeStringField("msg", value.getMessage());
		gen.writeStringField("data", value.getErrorCode());
		gen.writeEndObject();
	}

}
